#include "EPhysics.h"
#include "EGameTime.h"
#include "EPhAllocator.h"
#include <cooking/NxCooking.h>

#include "eCPP.h"
namespace engine
{

//------------------------------------------------------------
EPhysics *g_pPhysics                              = NULL;

//------------------------------------------------------------
EPhysics::EPhysics() :
m_pNxSDK( NULL )
{
  m_pNxSDK = NxCreatePhysicsSDK( NX_PHYSICS_SDK_VERSION, &g_PhAllocator, NULL );
  cTEST_NO_RETURN( m_pNxSDK != NULL );

#ifdef PHYSICS_USE_REMOTE_DEBUGGER
  if( m_pNxSDK->getFoundationSDK().getRemoteDebugger() != NULL )
    m_pNxSDK->getFoundationSDK().getRemoteDebugger()->connect ("localhost", 5425);
#endif
  m_pNxSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);

  cTEST_NO_RETURN( NxInitCooking() );
  //m_pNxSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1.0f);
}

//------------------------------------------------------------
EPhysics::~EPhysics()
{
  NxCloseCooking();
  for( vector<EPhScene*>::iterator it = m_vScenes.begin(); it != m_vScenes.end(); ++it )
    delete *it;
  m_vScenes.clear();

  if( m_pNxSDK != NULL )
    m_pNxSDK->release();
}

//------------------------------------------------------------
EPhScene*     EPhysics::createScene(const EPhSceneDesc& sceneDesc )
{
  EPhScene* pScene = new EPhScene( sceneDesc );
  cASSERT( pScene != NULL );
  m_vScenes.push_back( pScene );
  return pScene;
}

//------------------------------------------------------------
void    EPhysics::destroyScene(EPhScene &scene)
{
  for( vector<EPhScene*>::iterator it=m_vScenes.begin(); it != m_vScenes.end(); it++ )
  {
    if( *it == &scene )
    {
      m_vScenes.erase( it );
      break;
    }
  }
  delete &scene;
}

} // end namespace